<template>
	<view v-if="goods_detail.goods_name" class="goods-detail-box">
		<!-- 渲染轮盘图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :circular="true">
			<swiper-item v-for="(item,i) in goods_detail.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 渲染商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="goods-pirce">￥{{ goods_detail.goods_price}}</view>
			<!-- 商品主体 -->
			<view class="goods-body">
				<!-- 商品名字 -->
				<view class="goods-title">{{goods_detail.goods_name}}</view>
				<!-- 收藏图标 -->
				<view class="icon">
					<uni-icons type="star" size="18"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 快递服务 -->
			<view class="kd">快递: 免运费</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="goods_detail.goods_introduce"></rich-text>
		<!-- 商品导航组件 -->
		<view class="goods-nav">
			 <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations,mapGetters } from 'vuex'
	
	export default {
		  computed: {
		    // 调用 mapState 方法，把 m_cart 模块中的 cart 数组映射到当前页面中，作为计算属性来使用
		    // ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
		    ...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['total'])
		  },
		data() {
			return {
				goods_detail:{},
				 options: [{
				      icon: 'shop',
				      text: '店铺'
				    }, {
				      icon: 'cart',
				      text: '购物车',
				      info: 0
				    }],
				    // 右侧按钮组的配置对象
				    buttonGroup: [{
				        text: '加入购物车',
				        backgroundColor: '#ff0000',
				        color: '#fff'
				      },
				      {
				        text: '立即购买',
				        backgroundColor: '#ffa200',
				        color: '#fff'
				      }
				    ]
			};
		},
		watch:{
			total:{
				handler(newVal){
					const findResult = this.options.find((x) => x.text === '购物车')
					if(findResult){
						findResult.info = newVal
					}
				}
			},
			immediate: true
		},
		 onShow() {
		    // 在页面刚展示的时候，设置数字徽标
		    this.setBadge()
		  },
		onLoad(option) {
			const goods_id = option.goods_id
			this.getGoodsDetail(goods_id)
		},
		methods:{
		async getGoodsDetail(goods_id){
		const {data:res} = await uni.$http.get('/api/public/v1/goods/detail', { goods_id})
		if(res.meta.status !== 200) return uni.$showMge()
		// 解决商品详情图片的空白间隙
		res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
		this.goods_detail = res.message
			
			},
			preview(i){
				uni.previewImage({
					current:i,
					urls: this.goods_detail.pics.map(p => p.pics_big)
				})
			},
			onClick(e) {
			  // console.log(e)
			  if(e.content.text === '购物车'){
				  uni.switchTab({
				  	url: '/pages/cart/cart'
				  })
			  }
			},
			//将mutations添加的方法addToCart映射到当前页面
			...mapMutations('m_cart', ['addToCart']),
			// 为导航组件绑定事件
			buttonClick(e){
				if(e.content.text === '加入购物车'){
					const goods={
						goods_id: this.goods_detail.goods_id,
						goods_name: this.goods_detail.goods_name,
						goods_price: this.goods_detail.goods_price,
						goods_count:1,
						goods_small_logo: this.goods_detail.goods_small_logo,
						goods_state:true
					}
					this.addToCart(goods)
				}
			},
			  // 调用 uni.setTabBarBadge() 方法，为购物车设置右上角的徽标
		 setBadge() {
		   // 调用 uni.setTabBarBadge() 方法，为购物车设置右上角的徽标
		   uni.setTabBarBadge({
		     index: 2,
		     text: this.total + '', // 注意：text 的值必须是字符串，不能是数字
		   })
		 },
		}
	}
</script>

<style lang="scss">
.goods-detail-box{
		padding-bottom: 50px;
}
swiper{
	height: 750rpx;
	image{
		width: 100%;
		height: 100%;
	}
}
.goods-info-box{
	padding: 10px 10px;
	padding-right: 0;
}
.goods-pirce{
	margin: 5px 0;
	font-size: 18px;
	color: #c00000;
}
.goods-body{
	display: flex;
	justify-content: space-between;
	
	
}
.goods-title{
	padding-right: 10px;
	font-size: 12px;
}
.icon{
	display: flex;
	width: 100px;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	font-size: 13px;
	color: gray;
	
}
.kd{
	margin: 5px 0;
	font-size: 12px;
	color: gray;
}
.goods-nav{
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	
}
</style>
